package com.pzcnet.model;

import com.pzcnet.util.JSONUtil;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by LK on 2017/9/13.
 */
public class Payload extends HashMap<String, Object> {

    public Payload() {
    }

    public Payload(String jsonStr) {
        Map<String, Object> map = JSONUtil.parseJSON(jsonStr, Map.class);
        if (map != null) {
            this.putAll(map);
        }
    }

    public Payload addParameter(String key, Object value) {
        this.put(key, value);
        return this;
    }

    public Payload addObject(Object object) {
        String jsonStr = JSONUtil.getJSON(object);
        Map map = JSONUtil.parseJSON(jsonStr, Map.class);
        this.putAll(map);
        return this;
    }

    public String toJSON() {
        return JSONUtil.getJSON(this);
    }

    public static PayloadBuilder builder() {
        return new PayloadBuilder();
    }

    public static class PayloadBuilder {

        private Payload payload = new Payload();

        public PayloadBuilder addParameter(String key, Object value) {
            payload.addParameter(key, value);
            return this;
        }

        public PayloadBuilder addObject(Object object) {
            payload.addObject(object);
            return this;
        }

        public Payload build() {
            return payload;
        }
    }

}
